package com.assignwork.vo;

import com.assignwork.util.BigDecimalSerializer;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.springblade.core.tool.utils.DateUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springblade.core.tool.utils.Func;
import org.springframework.format.annotation.DateTimeFormat;

/**
 * DailyPlanVO
 */
@Data
@ApiModel(value = "DailyPlanVO对象", description = "日计划")
public class DailyPlanVO {

    /** 主键id;主键id */
    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty(value = "主键id")
    private Long id;
    
    /** 创建时间 */
    @DateTimeFormat(pattern = DateUtil.PATTERN_DATETIME)
    @JsonFormat(pattern = DateUtil.PATTERN_DATETIME)
    @ApiModelProperty(value = "创建时间")
    private LocalDateTime createTime;
    
    /** 更新时间 */
    @DateTimeFormat(pattern = DateUtil.PATTERN_DATETIME)
    @JsonFormat(pattern = DateUtil.PATTERN_DATETIME)
    @ApiModelProperty(value = "更新时间")
    private LocalDateTime updateTime;
    
    /** 计划日期 */
    @DateTimeFormat(pattern = DateUtil.PATTERN_DATE)
    @JsonFormat(pattern = DateUtil.PATTERN_DATE)
    @ApiModelProperty(value = "计划日期")
    private LocalDate planDate;
    
    /** 元数据ID */
    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty(value = "元数据ID")
    private Long sourceId;
    
    /** 船舶 */
    @ApiModelProperty(value = "船舶")
    private String shipName;

    private String voyage;
    
    /** 泊位 */
    @ApiModelProperty(value = "泊位")
    private String shipBerthKey;
    private String shipBerthName;

    /** 货类 */
    @ApiModelProperty(value = "货类")
    private String cargoType;
    private List<CargoCategoryVO> cargoTypeList;
    private String cargoTypeName;

    /** 进出口 */
    @ApiModelProperty(value = "进出口")
    private String impExp;
    private String impExpName;

    /** 装卸 */
    @ApiModelProperty(value = "装卸")
    private String workTypeKey;
    private String workTypeName;

    /** 散杂货 */
    @ApiModelProperty(value = "散杂货")
    private String packageBulkKey;
    private String packageBulkName;

    /** 总计划吨 */
    @ApiModelProperty(value = "总计划吨")
    private BigDecimal planTotal;
    
    /** 备注 */
    @ApiModelProperty(value = "备注")
    private String remark;
    
    /** 作业顺序 */
    @ApiModelProperty(value = "作业顺序")
    private Integer workOrder;

    private Integer isEmphasis;

    private Integer status;

    private List<AssemblyLineVO> assemblyLine;

    /** voyageId */
    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty(value = "voyageId")
    private Long voyageId;

    /** 船预做吨 */
    @ApiModelProperty(value = "船预做吨")
    @JsonSerialize(using = BigDecimalSerializer.class)
    private BigDecimal shipPreTon;

    /** 船结吨 */
    @ApiModelProperty(value = "船结吨")
    @JsonSerialize(using = BigDecimalSerializer.class)
    private BigDecimal shipFinishTon;

    public String getPlanNightLineTon() {
        if (Func.isNotEmpty(assemblyLine)) {
            return assemblyLine.stream().filter(AssemblyLineVO::plan).filter(AssemblyLineVO::isNight).map(AssemblyLineVO::getPlannedQuantity).reduce(BigDecimal.ZERO, BigDecimal::add).stripTrailingZeros().toPlainString();
        }
        return null;
    }

    public Long getPlanNightLineCount() {
        if (Func.isNotEmpty(assemblyLine)) {
            return assemblyLine.stream().filter(AssemblyLineVO::plan).filter(AssemblyLineVO::isNight).map(AssemblyLineVO::getAllStartedCabin).flatMap(Collection::stream).map(StartedCabinVO::getGroupFg).distinct().count();
        }
        return null;
    }

    public String getPlanDayLineTon() {
        if (Func.isNotEmpty(assemblyLine)) {
            return assemblyLine.stream().filter(AssemblyLineVO::plan).filter(AssemblyLineVO::isDay).map(AssemblyLineVO::getPlannedQuantity).reduce(BigDecimal.ZERO, BigDecimal::add).stripTrailingZeros().toPlainString();
        }
        return null;
    }

    public Long getPlanDayLineCount() {
        if (Func.isNotEmpty(assemblyLine)) {
            return assemblyLine.stream().filter(AssemblyLineVO::plan).filter(AssemblyLineVO::isDay).map(AssemblyLineVO::getAllStartedCabin).flatMap(Collection::stream).map(StartedCabinVO::getGroupFg).distinct().count();
        }
        return null;
    }

    public String getWorkTon() {
        if (Func.isNotEmpty(assemblyLine)) {
            return assemblyLine.stream().filter(AssemblyLineVO::work).map(AssemblyLineVO::getPlannedQuantity).reduce(BigDecimal.ZERO, BigDecimal::add).stripTrailingZeros().toPlainString();
        }
        return null;
    }

    public String getPlanTotalStr() {
        if (planTotal != null) {
            return planTotal.stripTrailingZeros().toPlainString();
        }
        return null;
    }

    public String getCargoTypeName() {
        if (Func.isBlank(cargoTypeName) && Func.isNotEmpty(cargoTypeList)) {
            return cargoTypeList.stream().map(CargoCategoryVO::getName).collect(Collectors.joining(","));
        }
        return cargoTypeName;
    }
}